package com.woniu.service.impl;

import com.woniu.entity.Student1;
import com.woniu.mapper.StudentDao;
import com.woniu.pojo.Student;
import com.woniu.pojo.StudentExample;
import com.woniu.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
@Transactional
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentDao studentDao;

    @Override
    public void add(Student student) {
        student.setStudentIntotime(new Date());
        student.setUserId(1);
        studentDao.insert(student);
    }

    @Override
    public void deleteById(Integer studentId) {
        studentDao.deleteByPrimaryKey(studentId);
    }

    @Override
    public Student1 findOne(Integer studentId) {
        return studentDao.selectByPrimaryKey1(studentId);
    }

    @Override
    public Student findOne1(Integer studentId) {
        return studentDao.selectByPrimaryKey(studentId);
    }

    /**
     * 得到班级学生的人数
     * @return
     */
    @Override
    public Integer countByExample(Integer clazzId) {
        StudentExample studentExample=new StudentExample();
        if (clazzId!=null&&clazzId!=0) {
            studentExample.createCriteria().andClassIdEqualTo(clazzId);
        }
        long l = studentDao.countByExample(studentExample);
        return (int)l;
    }

    @Override
    public void updateById(Student student) {
        studentDao.updateByPrimaryKeySelective(student);
    }

    @Override
    public List<Student> selectAll() {
        return studentDao.selectByExample(null);
    }

    @Override
    public List<Student> selectExample(Student student) {
        StudentExample example = new StudentExample();
        if (student.getStudentName() != null && !student.getStudentName().equals("")){
            example.createCriteria().andStudentNameLike("%" + student.getStudentName() + "%");
        }
        if (student.getClassId() != null){
            example.createCriteria().andClassIdEqualTo(student.getClassId());
        }
        return studentDao.selectByExample(example);
    }

    @Override
    public Student selectOne(Integer studentId) {
        return studentDao.selectByPrimaryKey(studentId);
    }

    @Override
    public List<Student1> findAll(Student student) {
        StudentExample example = new StudentExample();
        if (student != null){
            if (student.getStudentName() != null && !student.getStudentName().equals("")){
                example.createCriteria().andStudentNameLike("%" + student.getStudentName() + "%");
            }
            if (student.getClassId() != null){
                example.createCriteria().andClassIdEqualTo(student.getClassId());
            }
        }
        return studentDao.selectByExample1(example);
    }

    @Override
    public void deleteByExample(List<Integer> studentIds) {
        StudentExample example = new StudentExample();
        example.createCriteria().andStudentIdIn(studentIds);
        studentDao.deleteByExample(example);
    }

    @Override
    public List<Student1> findGraduatedStudents(Student student) {
        StudentExample example = new StudentExample();
        if (student != null){
            if (student.getStudentName() != null && !student.getStudentName().equals("")){
                example.createCriteria().andStudentNameLike("%" + student.getStudentName() + "%");
            }
            if (student.getClassId() != null){
                example.createCriteria().andClassIdEqualTo(student.getClassId());
            }
        }
        return studentDao.findGraduatedStudents(example);
    }

}
